package vpfarm;

import java.io.File;
import java.io.IOException;

import vpfarm.gui.JobGUI;
import vpfarm.master.Master;
import vpfarm.slave.Slave;

import jtools.ai.agenten.Plattform;
import jtools.files.TextFile;
import jtools.network.mqueue.Adress;
import jtools.network.mqueue.MessageException;

public class FileParser {
	public static void parse(String fileName) throws IOException, MessageException{
		TextFile tf = new TextFile();
		tf.readFile(new File(fileName), false);
		String line = tf.getNextLine();
		int port = 0;
		String host = null;
		if (line.toLowerCase().contains("master")){
			while(tf.hasNext()){
				 line = tf.getNextLine();
				 if (line.contains("-p"))
					 port = Integer.parseInt(line.split(" ")[1]);
				 if (line.contains("-h"))
					 host = line.split(" ")[1];
			 }			 
			if (port != 0 && host != null){
				 Plattform p = new Plattform(new Adress(host, port), null, null);
				 Master m = new Master(new Adress(host, port));
				 new JobGUI(host, port);
			}else
				throw new NullPointerException("Unbekannter Host oder unbekannter Port "+host+":"+port);
		}else{
			while(tf.hasNext()){
				 line = tf.getNextLine();
				 if (line.contains("-p"))
					 port = Integer.parseInt(line.split(" ")[1]);
				 if (line.contains("-h"))
					 host = line.split(" ")[1];
			 }
			if (port != 0 && host != null)
				new Slave(new Adress(host, port));
			else
				throw new NullPointerException("Unbekannter Host oder unbekannter Port "+host+":"+port);
		}
	}
}
